﻿using UnityEngine;
using UnityEditor;
namespace LitBT
{
    /// <summary>
    /// 自定义行为树运行器面板
    /// </summary>
    [CustomEditor(typeof(BehaviourTreeRunner))]
    public class BehaviourTreeRunnerInspector : Editor
    {
        SerializedProperty tree;
        SerializedProperty autoPlay;
        SerializedProperty updateMode;
        SerializedProperty intervalTime;
        private void OnEnable()
        {
            tree = serializedObject.FindProperty("tree");
            autoPlay = serializedObject.FindProperty("autoPlay");
            updateMode = serializedObject.FindProperty("updateMode");
            intervalTime = serializedObject.FindProperty("intervalTime");
        }
        public override void OnInspectorGUI()
        {
            EditorGUILayout.PropertyField(tree, new GUIContent("行为树"));
            EditorGUILayout.PropertyField(autoPlay, new GUIContent("自动运行"));
            EditorGUILayout.PropertyField(updateMode, new GUIContent("更新方式"));
            if(updateMode.enumValueIndex == (int)UpdateMode.TimeInterval)
            {
                EditorGUILayout.PropertyField(intervalTime,new GUIContent("间隔时间"));
            }
            serializedObject.ApplyModifiedProperties();
            OnPreviewBtnClick(target as BehaviourTreeRunner);
        }

        /// <summary>
        /// 预览按钮
        /// </summary>
        /// <param name="treeRunner"></param>
        private void OnPreviewBtnClick(BehaviourTreeRunner treeRunner)
        {
            GUILayout.Space(10);
            if (GUILayout.Button("查看行为树"))
            {
                var window = EditorWindow.GetWindow<BehaviourTreeEditor>();
                window.ShowTreeGraph(treeRunner.tree);
            }
        }
    }
}